package com.vegetable.modules.service.finance.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.finance.PaymentLog;
import com.vegetable.modules.entity.finance.dto.PaymentLogDTO;
import com.vegetable.modules.entity.finance.dto.PaymentLogQueryDTO;
import com.vegetable.modules.mapper.finance.PaymentLogMapper;
import com.vegetable.modules.service.finance.PaymentLogService;
import com.vegetable.modules.service.finance.IPaymentLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * 支付日志服务实现类
 *
 * @author Qoder
 */
@Slf4j
@Service
public class PaymentLogServiceImpl extends ServiceImpl<PaymentLogMapper, PaymentLog> implements PaymentLogService, IPaymentLogService {

    @Override
    public R<IPage<PaymentLogDTO>> queryPaymentLogs(PaymentLogQueryDTO queryDTO) {
        try {
            // 构建查询条件
            QueryWrapper<PaymentLog> wrapper = new QueryWrapper<>();
            
            // 添加查询条件
            if (queryDTO.getPaymentId() != null) {
                wrapper.eq("payment_id", queryDTO.getPaymentId());
            }
            
            if (queryDTO.getDocNo() != null && !queryDTO.getDocNo().isEmpty()) {
                wrapper.eq("doc_no", queryDTO.getDocNo());
            }
            
            if (queryDTO.getStatus() != null) {
                wrapper.eq("status", queryDTO.getStatus());
            }
            
            if (queryDTO.getChannel() != null) {
                wrapper.eq("channel", queryDTO.getChannel());
            }
            
            if (queryDTO.getPayerName() != null && !queryDTO.getPayerName().isEmpty()) {
                wrapper.like("payer_name", queryDTO.getPayerName());
            }
            
            if (queryDTO.getPayeeName() != null && !queryDTO.getPayeeName().isEmpty()) {
                wrapper.like("payee_name", queryDTO.getPayeeName());
            }
            
            if (queryDTO.getPayStartTime() != null) {
                wrapper.ge("pay_time", queryDTO.getPayStartTime());
            }
            
            if (queryDTO.getPayEndTime() != null) {
                wrapper.le("pay_time", queryDTO.getPayEndTime());
            }
            
            // 按创建时间倒序排列
            wrapper.orderByDesc("create_time");
            
            // 分页查询
            IPage<PaymentLog> page = new Page<>(queryDTO.getPage(), queryDTO.getLimit());
            IPage<PaymentLog> resultPage = this.page(page, wrapper);
            
            // 转换为DTO
            IPage<PaymentLogDTO> dtoPage = resultPage.convert(paymentLog -> {
                PaymentLogDTO dto = new PaymentLogDTO();
                BeanUtils.copyProperties(paymentLog, dto);
                return dto;
            });
            
            return R.data(dtoPage, "查询成功");
        } catch (Exception e) {
            log.error("查询支付日志时发生异常", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }

    @Override
    public R<PaymentLogDTO> getPaymentLogById(Long id) {
        try {
            PaymentLog paymentLog = this.getById(id);
            if (paymentLog == null) {
                return R.fail("支付日志不存在");
            }
            
            PaymentLogDTO dto = new PaymentLogDTO();
            BeanUtils.copyProperties(paymentLog, dto);
            
            return R.data(dto, "查询成功");
        } catch (Exception e) {
            log.error("查询支付日志详情时发生异常", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
}